
# Copyright (c) 2014, Franklin Brauning V.  All rights reserved.
# 
#    El desarrollo inicial de esta utilidad fue producido
#    por Franklin Brauning <brauningbar@gmail.com>
# 
#    Si consideras que este codigo puede serte util, eres libre
#    de usarlo a conveniencia, pero no olvides mantener la licencia
#    y dar credito al autor.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#   * Redistributions of source code must retain the above copyright notice, this
#     list of conditions and the following disclaimer.
# 
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
# 
#   * Neither the name of the author nor the names of its contributors may
#     be used to endorse or promote products derived from this software without
#     specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from django.db import models
from .base import *

class constant(abstractmodel):
	''' Guards a model to provent modifications after save
	'''
	class Meta:
		abstract = True

	def save(self):
		if self.id:
			self.trace("constant.save:not saving constant model")
		else:
			super(constant, self).save()

class constantable(abstractmodel):
	''' Guards a model instance to prevent .save to modify the register
	'''
	class Meta:
		abstract = True

	def markconstant(self, mark=True):
		self._constant = mark

	def save(self):
		if getattr(self, '_constant', None):
			super(constantable, self).save()
		else:
			self.trace("constantable:not saving model marked constant")

class fixable(abstractmodel):
	''' Guards a model to prevent modifications if the fixed flag is set
	'''
	class Meta:
		abstract = True

	fixed = models.BooleanField(default=False, editable=False)

	def unfix(self):
		if self.fixed:
			self.fixed = False
		else:
			self.trace("fixable.unfix:not unfixing already unfixed model")

	def fix(self):
		if self.fixed:
			self.trace("fixable.fix:not fixing already fixed model")
		else:
			self.markfixed = True

	def save(self, *a, **kw):
		if self.fixed:
			self.trace("fixable.save:saving fixed model does nothing")
		else:
			if getattr(self, 'markfixed', None):
				self.fixed = True
			super(fixed, self).save(*a, **kw)
			delattr(self, 'markfixed')
